//
//  HBPhotoController.m
//  HBPhotoPicker
//
//  Created by hibo on 2019/5/20.
//  Copyright © 2019 hibo. All rights reserved.
//

#import "HBPhotoController.h"
#import "HBPhotoListController.h"

@interface HBPhotoController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableview;
@end

@implementation HBPhotoController

-(UITableView *)tableview{
    if (_tableview==nil) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, HBScreenWidth, HBScreenHeight) style:UITableViewStylePlain];
        _tableview.rowHeight = 100;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.tableFooterView = [UIView new];
        [_tableview registerClass:[HBPhotoCell class] forCellReuseIdentifier:@"cell"];
        [self.view addSubview:_tableview];
    }
    return _tableview;
}
-(void)cancelBtn{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"选择图片";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelBtn)];
    self.navigationItem.rightBarButtonItem = right;
    
    if (_photoManager.collectionList.count>0) {
        [self goIntoListViewRow:0 Animated:NO];
        [self.tableview reloadData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _photoManager.collectionList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HBPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    HBAlbumModel *model = _photoManager.collectionList[indexPath.row];
    [HBPhotoManager getPhotoInfoWithAsset:model.coverAsset targetSize:CGSizeMake(100*HBScreenScale, 100*HBScreenScale) result:^(UIImage * _Nonnull image, PHAssetMediaType mediaType) {
        cell.imageview.image = image;
    }];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@ （%zi）",model.title,model.number];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self goIntoListViewRow:indexPath.row Animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//进入图片列表
-(void)goIntoListViewRow:(NSInteger)row Animated:(BOOL)animated{
    HBAlbumModel *model = _photoManager.collectionList[row];
    HBPhotoListController *list = [[HBPhotoListController alloc] init];
    list.albumModel = model;
    [self.navigationController pushViewController:list animated:animated];
    __weak typeof(self) weakSelf = self;
    list.selectResult = ^(NSArray * _Nonnull assets) {
        weakSelf.selectResult(assets);
    };
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
}
-(void)dealloc{
    NSLog(@"%@ 被释放",self.class);
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


@implementation HBPhotoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
        _imageview.clipsToBounds = YES;
        _imageview.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:_imageview];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, HBScreenWidth-120, 100)];
        _titleLabel.numberOfLines = 0;
        [self.contentView addSubview:_titleLabel];
    }
    return self;
}

@end
